package com.topvision.common.serialize;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.topvision.common.enumeration.BaseEnum;
import com.topvision.common.utils.EnumUtils;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @author shengwm
 * @version V0.0.1
 * @Title: EnumSerialize
 * @Package com.topvision.common.serialize
 * @Description: TODO
 * @date 2017/4/23 15:12
 */
public class EnumSerialize extends JsonSerializer<Object> {

    @Override
    public void serialize(Object value, JsonGenerator jgen,
                          SerializerProvider provider) throws IOException,
            JsonProcessingException {

        Map<Object, Object> map = new HashMap<Object, Object>();
        BaseEnum enumObject = (BaseEnum) value;
        map.put(EnumUtils.OUTPUT_CODE, enumObject.getValue());
        map.put(EnumUtils.OUTPUT_NAME, enumObject.getLabel());
        jgen.writeObject(map);

    }
}